package com.foreknow.web;


import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Rule;
import com.foreknow.entity.Scheduling;
import com.foreknow.entity.User;
import com.foreknow.service.SchedulingService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//医生排班管理
@RestController
@RequestMapping("/scheduling")
public class SchedulingController {

    @Autowired
    private SchedulingService schedulingService;

    //展示时间区间的排班信息 参数 科室deptid 开始时间starttime 结束时间 overtime
    @ApiOperation("展示时间区间的排班信息")
    @RequestMapping(value = "/SchedulingService",method = RequestMethod.POST)
    public String findByDeptidByDate(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        String starttime = (String) para.get("starttime");
        String overtime = (String) para.get("overtime");
        Integer deptid = (Integer) para.get("deptid");

        Integer pageNum = (Integer)para.get("pageNum");
        Integer pageSize =  (Integer)para.get("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        List<Scheduling> schedulings = schedulingService.selectByCondition(starttime, overtime, deptid);
        PageInfo<Scheduling> pageInfo = new PageInfo<Scheduling>(schedulings);

        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();

        hs.put("meta",new Constance("msg",200));
        hs.put("info",pageInfo);

        return objectMapper.writeValueAsString(hs);
    }


    //删除该科室下的排班信息  参数  科室deptid 开始时间starttime
    @ApiOperation("删除该科室下的排班信息")
    @RequestMapping(value = "/deleteByDeptidByDate",method = RequestMethod.POST)
    public String deleteByDeptidByDate(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();

        String starttime = (String) para.get("starttime");
        Integer deptid = (Integer) para.get("deptid");
        int i = schedulingService.deleteByConditionByLogic(starttime, deptid);
        //判断是否成功
        if (i>=0){
            hs.put("meta",new Constance("msg",200));
            return objectMapper.writeValueAsString(hs);
        }
        //失败
        hs.put("meta",new Constance("msg",404));
        return objectMapper.writeValueAsString(hs);
    }


    //根据排班规则生成排班计划信息 参数 科室deptid 规则名List<rulename> 开始时间starttime 结束时间 overtime
    @ApiOperation("根据排班规则生成排班计划信息")
    @RequestMapping(value = "/saveSchedulingByRule",method = RequestMethod.POST)
    public String saveSchedulingByRule(@RequestBody Map<String,Object> para) throws JsonProcessingException {

        HashMap<String,Object> hs=new HashMap<>();
        ObjectMapper objectMapper=new ObjectMapper();

        String starttime = (String) para.get("starttime");
        String overtime = (String) para.get("overtime");
        List<Rule> rulelist = (List<Rule>) para.get("rulelist");

        //由LinkedHashMap 转为 entity
        List<Rule> rules = JSON.parseArray(JSON.toJSONString(rulelist), Rule.class);

        int insert = schedulingService.insert(starttime, overtime, rules);

        //判断是否成功
        if (insert>0){
            hs.put("meta",new Constance("msg",200));
            return objectMapper.writeValueAsString(hs);
        }
        //失败
        hs.put("meta",new Constance("msg",404));
        return objectMapper.writeValueAsString(hs);
    }



}
